package com.yunxi.rocketMq.producer;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.SendResult;
import com.yunxi.pms.cache.service.ICacheManageService;
import com.yunxi.rocketMq.config.RocketMQConfig;
import com.yunxi.sto.pojo.VehiclePdiBillCheckVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by caoyong on 2019/09/24
 */
@Component
public class StoPdiCheckProducer {

    /*public static void main(String[] args) {
       Boolean[] isContinue = {true};
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                isContinue[0] = false;
            }
        }, 1000);
        int i = 1;
        while (isContinue[0]){
            i++;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(i+":"+simpleDateFormat.format(new Date()));
        }


    }*/

    private static final Logger logger = LoggerFactory.getLogger(StoPdiCheckProducer.class);

    @Autowired
    private RocketMQConfig rocketMQConfig;
    @Value("${aliyun.rocketMQ.sto.pdi.topic}")
    private String topic;
    @Value("${aliyun.rocketMQ.sto.pdi.tag}")
    private String tag;
    @Value("${cache.switch}")
    private Boolean cacheSwitch;

    @Autowired
    ICacheManageService iCacheManageService;

    /**
     * 普通消息
     * 同步发送
     * send 同步发送,sendAsync 异步发送,sendOneway 单向发送
     * @param vehiclePdiBillCheckVO
     * @return
     */
    public boolean sendMessage(VehiclePdiBillCheckVO vehiclePdiBillCheckVO) {
        final Boolean[] isContinue = {true};
        Producer producer = rocketMQConfig.getProducer();
        String msg = JSONObject.toJSONString(vehiclePdiBillCheckVO);
        try {
            Message message = new Message(topic, tag, msg.getBytes());
            SendResult result = producer.send(message);
            if(cacheSwitch){
                //设置1秒钟查询缓存时间
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        isContinue[0] = false;
                    }
                }, 1000);
                //如果命中缓存则立即结束循环
                while (isContinue[0]){
                    Boolean cache = iCacheManageService.getCache(vehiclePdiBillCheckVO.getPdiBillId());
                    if(cache != null){
                        //清除缓存
                        iCacheManageService.clearByKey(vehiclePdiBillCheckVO.getPdiBillId());
                        isContinue[0] = false;
                        if(cache){
                            //消费成功
                            return true;
                        }
                        else {
                            //消费失败
                            return false;
                        }
                    }
                }
                logger.info("推送PDI检查MQ消息完成"+JSONObject.toJSONString(result));
            }
            logger.info("推送PDI检查MQ消息完成"+JSONObject.toJSONString(result));
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
